package com.bawei.rk_01.address

import androidx.lifecycle.viewModelScope
import com.bawei.base.utils.SingLiveData
import com.bawei.base.utils.bean.AddressEntity
import com.bawei.base.utils.bean.AddresslisetEntity
import com.bawei.base.utils.bean.Res
import com.bawei.base.utils.mvvm.BaseViewModel
import com.bawei.base.utils.param.Paddress
import kotlinx.coroutines.launch
import retrofit2.http.Query

class AddressViewModel : BaseViewModel() {

    val addresssuccess = SingLiveData<Res<AddressEntity>>()
    val addressfail = SingLiveData<String>()

    fun address(paddress: Paddress){
        viewModelScope.launch {
            try {
                addresssuccess.postValue(apiService.address(paddress))
            }catch (e:Exception){
                addressfail.postValue(e.message.toString())
            }
        }
    }

    //渲染全部地址列表
    val listsuccess = SingLiveData<Res<AddresslisetEntity>>()
    val listfail = SingLiveData<String>()
    fun list(limit:Int,page:Int){
        viewModelScope.launch {
            try {
                listsuccess.postValue(apiService.addresslist(limit,page))
            }catch (e:Exception){
                listfail.postValue(e.message.toString())
            }
        }
    }

    //默认地址
    val morensuccess = SingLiveData<Res<String?>>()
    val morenfail = SingLiveData<String>()
    fun moren(id:Int){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,Any>()
                map["id"] = id
                morensuccess.postValue(apiService.moren(mapToBody(map)))
            }catch (e:Exception){
                morenfail.postValue(e.message.toString())
            }
        }
    }

    //删除地址
    val delsuccess = SingLiveData<Res<String?>>()
    val delfail = SingLiveData<String>()
    fun deladdress(id:Int){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,Any>()
                map["id"] = id
                delsuccess.postValue(apiService.deladdress(mapToBody(map)))
            }catch (e:Exception){
                delfail.postValue(e.message.toString())
            }
        }
    }

}